package claybird.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import claybird.dao.HandicapDao;
import claybird.dao.PersonneDao;
import claybird.dom.Handicap;
import claybird.dom.Handicap.Categorie;
import claybird.dom.Person;

@Controller
public class PersonneController {
	@Autowired PersonneDao personneDao;
	@Autowired HandicapDao handicapDao;
	
	
	@RequestMapping("/personne/addHandicap")
	public ModelAndView addHandicap(@RequestParam("id") Long id,
									@RequestParam("year") Short year,
									@RequestParam("handicap") Float handicap,
									@RequestParam("categorie") String categorie){
		Person pers =personneDao.find(id);
		Handicap hcp = handicapDao.findByYear(year,pers);
		if (hcp == null){
			hcp = new Handicap(pers);
			hcp.setYear(year);
			hcp.setCurrentHandicap(handicap);
		}
		hcp.setCategorie(Categorie.valueOf(categorie));
		hcp.setPreviousHandicap(handicap);
		handicapDao.persist(hcp);
		personneDao.persist(pers);
		ModelAndView mv = new ModelAndView("showpersonlist","listPersonne",personneDao.getAllPersonne());
		return mv;
	}
	
	@RequestMapping("/personne/vue")
	public ModelAndView showUser(){
		ModelAndView mv = new ModelAndView("showpersonlist","listPersonne",personneDao.getAllPersonne());
		return mv;
	}
	@RequestMapping("/personne/ShowEditForm")
	public ModelAndView showUser(@RequestParam(value="id",required=false) Long id){
		ModelAndView mv = new ModelAndView("edition_personne");
		if (id != null){
			Person user = personneDao.find(id);
			mv.addObject("personne", user);
		}
		return mv;
	}
	@RequestMapping("/personne/Add")
	public ModelAndView addUser(@RequestParam("name") String name,@RequestParam("forName") String forName
								,@RequestParam(value ="manager",required=false)Boolean manager,@RequestParam(value ="guest",required=false)Boolean invite ){
		if (manager == null){
			manager = false;
		}
		if (invite == null){
			invite = false;
		}
		Person pers= new Person(name,forName,manager,invite);
		personneDao.persist(pers);
		return  new ModelAndView("edition_handicap","personne", pers);
	}
	@RequestMapping("/personne/Delete")
	public ModelAndView deleteUser(@RequestParam(value="id") Long id ){
		if (id != null){
			Person user = personneDao.find(id);
			for(Handicap hand : user.getListHandicap()){
				handicapDao.remove(hand);
			}
			personneDao.remove(user);
		}
		return this.showUser();	
	}
	@RequestMapping("/personne/editHandicap")
	public ModelAndView editHandicap(@RequestParam(value="id") Long id ){
		Person user = personneDao.find(id);
		return  new ModelAndView("edition_handicap","personne", user);
	}
}
